package com.sakura.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.dto.InsertOrUpdateManagerDTO;
import com.sakura.dto.ManagerDTO;
import com.sakura.dto.ManagerLoginDTO;
import com.sakura.dto.QueryManagerDTO;
import com.sakura.entity.Manager;
import com.sakura.service.ManagerService;
import com.sakura.mapper.ManagerMapper;
import com.sakura.util.EntityConverter;
import com.sakura.util.R;
import com.sakura.vo.ManagerVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Sakura
 * @description 针对表【manager】的数据库操作Service实现
 * @createDate 2023-11-22 15:49:03
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager>
        implements ManagerService {

    @Override
    public Page<ManagerVO> getPage(QueryManagerDTO queryManagerDTO) {
        LambdaQueryWrapper<Manager> managerWrapper = new LambdaQueryWrapper<>();

        managerWrapper.eq(queryManagerDTO.getId() != null && queryManagerDTO.getId() != 0, Manager::getId, queryManagerDTO.getId());

        managerWrapper.like(queryManagerDTO.getPhone() != null && !"".equals(queryManagerDTO.getPhone()), Manager::getPhone, queryManagerDTO.getPhone());

        managerWrapper.like(queryManagerDTO.getName() != null && !"".equals(queryManagerDTO.getName()), Manager::getName, queryManagerDTO.getName());

        Page<Manager> page = this.page(new Page<>(queryManagerDTO.getCurrent(), queryManagerDTO.getPageSize()), managerWrapper);

        Page<ManagerVO> pageVo = EntityConverter.copyAndGetSingle(page, Page.class, "records");
        List<ManagerVO> voList = EntityConverter.copyAndGetList(page.getRecords(), ManagerVO.class);
        pageVo.setRecords(voList);
        return pageVo;
    }

    /**
     * 登录
     *
     * @param loginDTO :
     * @return void
     * @author Sakura
     * @date 2023/11/23 9:08
     */
    @Override
    public R login(ManagerLoginDTO loginDTO) {
        Manager manager = this.getOne(new LambdaQueryWrapper<Manager>()
                .eq(Manager::getUsername, loginDTO.getUsername()));
        if (Objects.isNull(manager)) {
            return R.error("该账号未注册，请注册后再登录");
        }
        if (!manager.getPassword().equals(loginDTO.getPassword())) {
            return R.error("密码错误，请重新输入");
        }
        return R.success(manager);
    }

    /**
     * 管理员注册
     *
     * @param registerDTO:
     * @return R
     * @author Sakura
     * @date 2023/11/23 9:21
     */
    @Override
    public R register(InsertOrUpdateManagerDTO registerDTO) {
        Manager managerDB = this.getOne(new LambdaQueryWrapper<Manager>()
                .eq(Manager::getUsername, registerDTO.getUsername()));
        if (Objects.nonNull(managerDB)) {
            return R.error("该账号已注册");
        }
        Manager manager = EntityConverter.copyAndGetSingle(registerDTO, Manager.class);
        boolean result = this.save(manager);
        return R.getResult(result);
    }

    @Override
    public R logout(ManagerDTO managerDTO) {
        //清除登录缓存
        return R.success("退出登录成功");
    }

    /**
     * 发送验证码
     * @param phone:
     * @return R
     * @author Sakura
     * @date 2024/4/3 17:16
     */
    @Override
    public R getCaptcha(String phone) {

        return R.success();
    }


}




